/**
 * Project: shuyin-sync
 * 
 * File Created at Mar 20, 2012
 * $Id$
 * 
 * Copyright 2008 6677bank.com Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * 6677bank Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with 6677bank.com.
 */
package com.bank6677.sync.csm.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.bank6677.sync.csm.dao.ApplicationDao;
import com.bank6677.sync.csm.domain.Application;
import com.bank6677.sync.csm.domain.ApplicationExt;
import com.bank6677.sync.csm.domain.ApplicationStatFlow;
import com.bank6677.sync.csm.domain.DropApplication;
import com.bank6677.sync.csm.domain.Reservation;
import com.bank6677.sync.csm.domain.SendInstitutionFlow;
import com.bank6677.sync.csm.domain.UrgeApp;
import com.bank6677.sync.csm.domain.old.LoanApply;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * TODO Comment of ApplicationDaoImpl
 * 
 * @author liyue
 * @version ApplicationDaoImpl.java Mar 20, 2012 3:06:17 PM
 */

@Repository
public class ApplicationDaoImpl implements ApplicationDao {

    @Autowired
    @Qualifier("newpayxSqlMapClient")
    private SqlMapClient newpayxClient;

    @Autowired
    @Qualifier("cmbankSqlMapClient")
    private SqlMapClient cmbankSqlMapClient;

    @SuppressWarnings("unchecked")
    @Override
    public List<LoanApply> findOldApplication(Integer start, Integer end) throws SQLException {

        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("start", start);
        map.put("end", end);

        return newpayxClient.queryForList("loan_apply.findAllOldApplication", map);
    }

    @SuppressWarnings("unchecked")
    public List<Application> findOldApplication2new(Integer start, Integer end) throws SQLException {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("start", start);
        map.put("end", end);
        return newpayxClient.queryForList("loan_apply.findAllOldApplicationWithDemandInfo", map);
    }
    
    @SuppressWarnings("unchecked")
    public List<Application> findOldApplication2new(Integer start, Integer end, String from, String to) throws SQLException {
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("start", start);
        map.put("end", end);
        map.put("from", from);
        map.put("to", to);
    	return newpayxClient.queryForList("loan_apply.findAllOldApplicationWithDemandInfo", map);
    }
    
    @SuppressWarnings("unchecked")
    public List<Application> findOldApplication2new(List<Long> ids) throws SQLException {
    	return newpayxClient.queryForList("loan_apply.findAllOldApplicationWithDemandInfo_ids", ids);
    }

    @Override
    public Integer countOldApplication(Map conditions) throws SQLException {
        return (Integer) newpayxClient.queryForObject("loan_apply.countAllOldApplication", conditions);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<UrgeApp> findOldUrgeByApplyId(Long applyId) throws SQLException {
        return newpayxClient.queryForList("loan_apply.findUrgesByAppId", applyId);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<ApplicationStatFlow> findOldOptLogsByApplyId(Long applyId) throws SQLException {
        return newpayxClient.queryForList("loan_apply.findLogsByAppId", applyId);
    }

    @SuppressWarnings("unchecked")
    public List<DropApplication> findOldDropFlowByAppId(Long applyId) throws SQLException {
        return newpayxClient.queryForList("loan_apply.findDropAppInStatOptByAppId", applyId);
    }

    @SuppressWarnings("unchecked")
    public List<Reservation> findOldReservationByAppId(Long applyId) throws SQLException {
        return newpayxClient.queryForList("loan_apply.findReservationsByAppId", applyId);
    }

    @SuppressWarnings("unchecked")
    public List<SendInstitutionFlow> findOldSendFlowByAppId(Long applyId) throws SQLException {
        return newpayxClient.queryForList("loan_apply.findSendInstutionFlowsByAppId", applyId);
    }

    @SuppressWarnings("unchecked")
    public ApplicationExt findAppExtsByAppId(Long applyId) throws SQLException {
        return (ApplicationExt) newpayxClient.queryForObject("loan_apply.findAppExtByAppId", applyId);
    }

    @Override
    public void saveApplication(Application application) throws SQLException {
        cmbankSqlMapClient.insert("loan_apply.addApplication", application);

        if (!CollectionUtils.isEmpty(application.getFlows())) {
            for (ApplicationStatFlow flow : application.getFlows()) {
                cmbankSqlMapClient.insert("loan_apply.addApplicationFlow", flow);
            }
        }

        if (!CollectionUtils.isEmpty(application.getUrges())) {
            for (UrgeApp urge : application.getUrges()) {
                cmbankSqlMapClient.insert("loan_apply.addApplicationUrge", urge);
            }
        }

        if (!CollectionUtils.isEmpty(application.getDrops())) {
            for (DropApplication drop : application.getDrops()) {
                cmbankSqlMapClient.insert("loan_apply.addDropApplication", drop);
            }
        }

        if (!CollectionUtils.isEmpty(application.getReservations())) {
            for (Reservation reserve : application.getReservations()) {
                cmbankSqlMapClient.insert("loan_apply.addReservations", reserve);
            }
        }

        if (!CollectionUtils.isEmpty(application.getSendFlow())) {
            for (SendInstitutionFlow flow : application.getSendFlow()) {
                cmbankSqlMapClient.insert("loan_apply.addSendInstitutionFlow", flow);
            }
        }
        
        if (application.getExt() != null) {
            cmbankSqlMapClient.insert("loan_apply.addAppExt", application.getExt());
        }
    }

    @Override
    public void batchSaveApplications(List<Application> applications) throws SQLException {
        cmbankSqlMapClient.startBatch();
        for (Application application : applications) {
            saveApplication(application);
        }
        cmbankSqlMapClient.executeBatch();
    }

}
